<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 600px;
            height: 3000px;
            margin: auto;
            background-color: red;
        }

        .ad {
            width: 150px;
            height: 500px;
            background-color: yellow;
            position: absolute;
            top: 200px;
            transition: all 1s;
        }

        .left {
            left: 0;
        }

        .right {
            right: 0;
        }
    </style>
</head>

<body>
    <div class="box"></div>
    <div class="ad left">
        <p>点击关闭广告 <span>X</span></p>
    </div>
    <div class="ad right">
        <p>点击关闭广告 <span>X</span></p>
    </div>

    <script>
        let ad = document.querySelectorAll(".ad");
        let close = document.getElementsByTagName("span");

        window.onscroll = function () {
            for (let i = 0; i < ad.length; i++) {
                ad[i].style.top =200+pageYOffset+"px";
            }
        }
        for(let i=0;i<close.length;i++){
            close[i].onclick=function(){
                ad[i].remove();
            }
        }
    </script>
</body>

</html>